package com.atguigu.gulimall.order.web;

import com.alipay.api.AlipayApiException;
import com.atguigu.gulimall.order.config.AlipayTemplate;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PayWebController {

    @Autowired
    AlipayTemplate alipayTemplate;

    @Autowired
    OrderService orderService;


    /**
     * 支付功能
     *      支付成功后，我们要跳到用户的订单列表页
     */
    @ResponseBody
    @GetMapping(value = "/payOrder",produces = "text/html")//明确指出返回值是一个html类型
    public String payOrder(@RequestParam("orderSn")String orderSn) throws AlipayApiException {
        //1、获取当前订单对应的支付数据
        PayVo payVo = orderService.getOrderPay(orderSn);

        //2、调用支付宝的SDK，完成支付
        //该方法会返回一个页面，将此页面直接交给浏览器即可（这个页面会向支付宝发送支付请求）
        String pay = alipayTemplate.pay(payVo);

        return pay;
    }


}
